//
//  LXPatientsHeaderView.m
//  bgms_d
//
//  Created by lachesis-zhu on 13/07/2017.
//  Copyright © 2017 lachesis-zhu. All rights reserved.
//

#import "LXPatientsHeaderView.h"

@interface LXPatientsHeaderView () <UIGestureRecognizerDelegate>


@end

@implementation LXPatientsHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self buildSubview];
    }
    
    return self;
}

- (void)buildSubview
{
    self.editButton = [[UIButton alloc] init];
    [self.editButton addTarget:self action:@selector(selectDown:) forControlEvents:UIControlEventTouchUpInside];
    [self.editButton setImage:[UIImage imageNamed:@"icon_zhengchang_default"] forState:UIControlStateNormal];
    [self.editButton setImage:[UIImage imageNamed:@"icon_xuanzhon_selected"] forState:UIControlStateSelected];
    [self addSubview:self.editButton];
    
    self.selectImage = [[UIImageView alloc] init];
    [self.selectImage setImage:[UIImage imageNamed:@"icon_xiala"]];
    self.positionType = RightType;
    [self addSubview:self.selectImage];
    
    self.showLab = [[UILabel alloc] init];
    [self.showLab setFont:[UIFont lx_systemFontOfSize:17]];
    [self.showLab setTextColor:kAppNormalTextColor];
    [self.showLab setTextAlignment:NSTextAlignmentLeft];
    [self addSubview:self.showLab];
    
    self.numberLab = [[UILabel alloc] init];
    [self.numberLab setFont:[UIFont lx_systemFontOfSize:15]];
    [self.numberLab setTextColor:kAppLBlackTextColor];
    [self.numberLab setTextAlignment:NSTextAlignmentRight];
    [self addSubview:self.numberLab];
    
    [self.editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset([LXAutoLength lx_L:10]);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(0, 30));
    }];
    [self.selectImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.editButton.mas_right).with.offset([LXAutoLength lx_L:5]);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake([LXAutoLength lx_L:20], [LXAutoLength lx_L:20]));
    }];
    [self.showLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.selectImage.mas_right).with.offset([LXAutoLength lx_L:15]);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake([LXAutoLength lx_L:200], [LXAutoLength lx_L:20]));
    }];
    [self.numberLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).with.offset([LXAutoLength lx_L:-15]);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake([LXAutoLength lx_L:80], [LXAutoLength lx_L:20]));
    }];
    
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    tapRecognizer.delegate = self;
    tapRecognizer.numberOfTouchesRequired = 1;
    tapRecognizer.numberOfTapsRequired = 1;
    [self addGestureRecognizer:tapRecognizer];
}

- (void)handleTap:(UIGestureRecognizer *)gesture {
    if (self.headerViewDelegate && [self.headerViewDelegate respondsToSelector:@selector(patientsHeaderView:event:)]) {
        [self.headerViewDelegate patientsHeaderView:self event:gesture];
    }
}

- (void)selectDown:(UIButton *)sender
{
    if (sender.isSelected) {
        sender.selected = NO;
    } else {
        sender.selected = YES;
    }
    if (self.headerViewDelegate && [self.headerViewDelegate respondsToSelector:@selector(patientsHeaderView:selectDown:)]) {
        [self.headerViewDelegate patientsHeaderView:self selectDown:sender];
    }
}

- (void)setEditing:(BOOL)editing
{
    if (editing) {
        [self.editButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(30);
        }];
    } else {
        [self.editButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0);
        }];
    }
    [self setNeedsUpdateConstraints];
    [UIView animateWithDuration:0.2 animations:^{
        [self layoutIfNeeded];
    }];
}

- (void)changeToLeftAnimated {
    self.positionType = RightType;
    [UIView animateWithDuration:0.5f animations:^{
        self.selectImage.transform = CGAffineTransformRotate(self.selectImage.transform, -M_PI_2);
    }];
}
- (void)changeToRightAnimated {
    self.positionType = BottomType;
    [UIView animateWithDuration:0.5f animations:^{
        self.selectImage.transform = CGAffineTransformRotate(self.selectImage.transform, M_PI_2);
    }];
}
@end

